<template>
  <el-dialog
    title="申请标签审核"
    :visible.sync="visible"
    width="35%"
    top="13vh"
    :before-close="closeHandle"
    :close-on-click-modal="false">
    <el-form :model="auditForm" :rules="auditRules" size="small" ref="auditForm" @keyup.enter.native="submitAuditHandle" label-width="85px">
      <el-form-item label="标签名字">
        <el-input v-model="tag.name" readonly class="width90percent"></el-input>
      </el-form-item>
      <el-divider content-position="center">审核</el-divider>
      <el-form-item label="" prop="bool">
        <el-radio-group v-model="auditForm.bool" @input="changeRadioHandle">
          <el-radio :label="true" border>添加</el-radio>
          <el-radio :label="false" border>拒绝</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序等级" prop="sort">
        <el-input :disabled="!auditForm.bool" v-model.number="auditForm.sort" maxlength="5" placeholder="请输入排序等级" class="width90percent"></el-input>
      </el-form-item>
      <el-form-item label="回复消息" prop="message">
        <el-input :disabled="!!auditForm.bool" v-model="auditForm.message" maxlength="50" show-word-limit clearable placeholder="请输入回复消息" class="width90percent"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="closeHandle(false)">取消</el-button>
      <el-button :disabled="buttonLoad" type="primary" @click="submitAuditHandle">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    name: "blog-tag-audit-dialog",
    data() {
      let checkSort = (rule, value, callback) => {
        if (this.auditForm.bool) {
          if (!value) {
            return callback(new Error('排序等级不能为空'));
          }
          if (!/^[+]?(\d+)$/.test(value)) {
            return callback(new Error('请输入正确的排序等级'))
          }
        }
        callback()
      }
      let checkMessage = (rule, value, callback) => {
        if (!this.auditForm.bool && !value) {
          return callback(new Error('回复不能为空'));
        }
        callback()
      }
      return {
        // 弹出框显示状态[true-显示 false-隐藏]
        visible: false,
        // 标签对象
        tag: {
          // 标签主键
          tagId: undefined,
          // 标签名称
          name: undefined
        },
        // 审核表单对象
        auditForm: {
          // 审核结果[true-通过 false-拒绝]
          bool: '',
          // 排序等级
          sort: '',
          // 审核拒绝，给申请用户发送的消息
          message: '',
        },
        /// 审核表单校验对象
        auditRules: {
          bool: [
            { required: true, message: '请选择审核结果', trigger: 'change' }
          ],
          sort: [
            { required: true, validator: checkSort, trigger: 'blur' },
          ],
          message: [
            { required: true, validator: checkMessage, trigger: 'blur' },
          ],

        },
        // 按钮是否可被点击[false-可以被点击 true-不可被点击]
        buttonLoad: false,
      }
    },
    methods: {
      /**
       * 初始化弹出框方法
       * @param {Object} tag 标签对象
       */
      init(tag) {
        // 打开弹出框
        this.visible = true;

        let _tag = this.tag;
        _tag.tagId = tag.tagId;
        _tag.name = tag.name;
      },
      /**
       * 获取最新排序等级方法
       */
      getSort() {
        this.$axios.$get('/blog/tag/next/sort')
          .then(({ data }) => {
            this.auditForm.sort = data;
          })
          .catch(() => {})
      },
      /**
       * 审核结果选择事件
       * @param {boolean} value 最新值
       */
      changeRadioHandle(value) {
        if (value) {
          // 添加
          this.auditForm.message = ''
          this.getSort();
          this.$refs.auditForm.clearValidate('message');
        } else {
          // 拒绝
          this.auditForm.sort = ''
          this.$refs.auditForm.clearValidate('sort');
        }
      },
      /**
       * 审核提交事件
       */
      submitAuditHandle() {
        this.$refs.auditForm.validate((valid) => {
          if (!valid) {
            return false;
          }
          // 按钮禁用
          this.buttonLoad = true;
          let data = {
            tagId: this.tag.tagId,
            ...this.auditForm
          }
          this.$axios.$post('/blog/tag/audit', data)
            .then(() => {
              this.notify('success', '操作成功', `’${this.tag.name}‘标签审核完成`)

              this.closeHandle(true);
            })
            .catch(err => {
              if (err.code === 409) {
                this.closeHandle(true);
              }
            })
            .finally(() => {
              // 按钮恢复可点击
              this.buttonLoad = false;
            })
        });
      },
      /**
       * 关闭弹出框事件
       * @param {boolean | function} bool 是否刷新父级组件数据[true-刷新 false-不刷新]
       */
      closeHandle(bool) {
        // 关闭弹出框
        this.visible = false;
        // 重置表单对象
        this.$refs.auditForm.resetFields();

        let _tag = this.tag;
        _tag.tagId = undefined;
        _tag.name = undefined;

        // 此处判断触发事件是否是弹出框自带关闭事件
        if (typeof bool === 'function') {
          bool = false
        }
        // 向上传递事件
        this.$emit('close-dialog', bool);
      }
    }
  }
</script>

<style scoped>
  .width90percent {
    width: 90%;
  }
</style>
